package tree.递归.剑指;

import po.TreeNode;

/**
 * @author songZiHao
 * @version 1.0.0
 * @ClassName 剑指Offer树的子结构26.java
 * @Description TODO https://leetcode-cn.com/problems/shu-de-zi-jie-gou-lcof/
 * @createTime 2021年03月28日 09:45:00
 */
public class 剑指Offer树的子结构26 {
    public boolean isSubStructure(TreeNode A, TreeNode B) {
        if (B == null) {
            return false;
        }
        return findSame(A, B);
    }

    private boolean findSame(TreeNode a, TreeNode b) {
        if (a == null) {
            return false;
        }
        return judje(a, b) || findSame(a.left, b) || findSame(a.right, b);
    }

    private boolean judje(TreeNode a, TreeNode b) {
        if (b == null) {
            return true;
        }
        if (a == null || a.val != b.val) {
            return false;
        }
        return judje(a.left, b.left) && judje(a.right, b.right);
    }
}
